<?php


namespace app\cms\model;


use app\cms\community\lib\QueryModel;
use app\cms\dto\companyInfo;
use iflow\Response;

class companyModel extends QueryModel
{

    protected $table = "company_table";

    public static function getCompanyInfo(): Response
    {
        $company = static::where([
            'companyId' => 1
        ]) -> cache('update_time') -> findOrEmpty();

        if ($company -> isExists()) {
            return message() -> success('获取企业信息成功', $company -> toArray());
        }
        return message() -> nodata('暂无企业信息');
    }

    public static function updateCompanyInfo(companyInfo $companyInfo): Response
    {
        $info = (array)$companyInfo;
        unset($info['companyId']);
        $info = array_change_key_case($info, CASE_LOWER);

        $company = static::where([
            'companyId' => 1
        ]) -> findOrEmpty();
        if ($company -> isExists()) {
            $company = static::cache('update_time')
                -> where([
                    'companyId' => 1
                ]) -> update($info);
            if ($company) {
                return message() -> success('更新企业信息成功', $company -> getChangedData());
            }
        }
        return message() -> success('更新企业信息成功', self::create($info) -> toArray());
    }

}